package com.engine4cloud.tdf.supervision.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.supervision.dto.create.SpOutApplicationCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.SpOutApplicationQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpOutApplicationResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpOutApplicationUpdateDTO;
import com.engine4cloud.tdf.supervision.service.SpOutApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 审批-外出申请管理-web
 *
 * @author wenjie
 * @date 2024-04-08 14:33:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/out")
@Tag(description = "OutApplicationWeb", name = "审批-外出申请管理-web")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SpOutApplicationWebController {

    private final SpOutApplicationService spOutApplicationService;

    /**
     * 分页查询
     *
     * @param dto 外出申请信息采集表-请求对象
     * @return R<PageInfo < SpOutApplicationResultDTO>>
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R<PageInfo<SpOutApplicationResultDTO>> pageList(@Valid @ParameterObject SpOutApplicationQueryDTO dto) {
        return R.ok(spOutApplicationService.pageList(dto));
    }

    /**
     * 通过id查询外出申请信息采集表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<SpOutApplicationResultDTO> getById(@PathVariable("id") Long id) {
        return R.ok(spOutApplicationService.getById(id));
    }

    /**
     * 创建外出申请
     *
     * @param dto 创建外出申请
     * @return R
     */
    @Operation(summary = "创建外出申请", description = "创建外出申请")
    @SysLog("创建外出申请")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('out_application_add')")
    public R<Boolean> create(@Valid @RequestBody SpOutApplicationCreateDTO dto) {
        return R.ok(spOutApplicationService.create(dto));
    }

    /**
     * 修改外出申请
     *
     * @param dto 修改外出申请
     * @return R
     */
    @Operation(summary = "修改外出申请", description = "修改外出申请")
    @SysLog("修改外出申请")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('out_application_edit')")
    public R<Boolean> update(@Valid @RequestBody SpOutApplicationUpdateDTO dto) {
        return R.ok(spOutApplicationService.update(dto));
    }

    /**
     * 上报
     *
     * @param id 请假记录id
     * @return R
     */
    @Operation(summary = "上报", description = "上报")
    @SysLog("上报")
    @PutMapping("/escalation/{id}")
    @PreAuthorize("@pms.hasPermission('out_application_edit')")
    public R<Boolean> escalation(@Valid @Schema(description = "请假记录id") @PathVariable("id") Long id) {
        return R.ok(spOutApplicationService.escalation(id));
    }

    /**
     * 通过id删除外出申请
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除外出申请", description = "通过id删除外出申请")
    @SysLog("通过id删除外出申请")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('out_application_del')")
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(spOutApplicationService.removeBatchByIds(CollUtil.toList(ids)));
    }
}